//
//  NSArray+LYAdd.m
//  LYDevelopKit
//
//  Created by 李阳 on 2018/5/26.
//

#import "NSArray+LYAdd.h"
#import "LYNSMacro.h"

LYSYNTH_DUMMY_CLASS(NSArray_LYAdd)

@implementation NSArray (LYAdd)


- (CGFloat)sum
{
    return [self _valueForKeyPath:@"@sum.floatValue"];
}
- (CGFloat)max
{
    return [self _valueForKeyPath:@"@max.floatValue"];
}
- (CGFloat)min
{
    return [self _valueForKeyPath:@"@min.floatValue"];
}
- (CGFloat)avg
{
    return [self _valueForKeyPath:@"@avg..floatValue"];
}
- (NSArray *)distinctUnionOfObjects
{
    return [self valueForKeyPath:@"@distinctUnionOfObjects.self"];
}

- (CGFloat)_valueForKeyPath:(NSString *)path
{
    return [[self valueForKeyPath:path] floatValue];
}



- (NSArray *)separatedByCount:(NSUInteger)max
{
    NSInteger count = self.count;
    if (count <= max) return @[self];
    NSMutableArray *temp = [NSMutableArray array];
    for (int i = 0; i < count; i += max) {
        [temp addObject:[self subarrayWithRange:(NSRange){i, MIN(max, count - i)}]];
    }
    return temp;
}
- (NSArray *)reverseArray
{
    return self.reverseObjectEnumerator.allObjects;
}
@end



@implementation NSMutableArray (LYAdd)


- (NSUInteger)addObjectOrderly:(id<LYCompareDelegate>)object
           primaryKeyPath:(NSString *)primaryKeyPath
                     ascending:(BOOL)ascending
{
    NSUInteger(^sortedWay)(void) = ^{
        NSUInteger idx = 0;
        NSInteger count = self.count;
        for (NSInteger i = 0; i < count; i++)
        {
            id<LYCompareDelegate> current = self[i];
            NSComparisonResult result = [current compare:object];
            /**
             current <= object   != NSOrderedDescending
             currnet >= object   != NSOrderedAscending
             current > object    == NSOrderedAscending
             current < object    == NSOrderedDescending
             current == object   == NSOrderedSame
             */
            if (count == 1)
            {
                if (ascending)
                {
                    // 升序
                    idx = result != NSOrderedDescending;
                }
                else
                {
                    // 降序
                    idx = result != NSOrderedAscending;
                }
                break;
            }
            
            if (i == count - 1)
            {
                // 遍历到最后一个元素
                if (ascending)
                {
                    idx = result != NSOrderedAscending ? 0 : count;
                }
                else
                {
                    idx = result != NSOrderedDescending ? 0 : count;
                }
                break;
            }
            
            id<LYCompareDelegate> next = self[i + 1];
            NSComparisonResult nextResult = [next compare:object];
            if (ascending)
            {
                if (result != NSOrderedDescending &&
                    nextResult != NSOrderedAscending)
                {
                    idx = i + 1;
                    break;
                }
            }
            else
            {
                if (result != NSOrderedAscending &&
                    nextResult != NSOrderedDescending)
                {
                    idx = i + 1;
                    break;
                }
            }
        }
        
        [self insertObject:object atIndex:idx];
        return idx;
    };
    
    if (primaryKeyPath)
    {
        NSArray *allObjects = [self valueForKeyPath:primaryKeyPath];
        if (![allObjects containsObject:object])
        {
            return sortedWay();
        }
    }
    else
    {
        return sortedWay();
    }
    return NSNotFound;
}

- (BOOL)replaceObjectPassingTest:(BOOL (^)(id _Nonnull, NSUInteger, BOOL * _Nonnull))predicate withObject:(id)object
{
    NSUInteger idx = [self indexOfObjectPassingTest:predicate];
    if (idx == NSNotFound)
    {
        return NO;
    }
    [self replaceObjectAtIndex:idx withObject:object];
    return YES;
}

@end

